<template>
  <div id="shopcar">
    <div id="content">
      <div class="header">
        <img src="./../../common/img/logo-round.png" class="header_logo" />
        <span>购物车</span>
      </div>
      <div class="cont_title">
        <span>全部商品</span>
      </div>
      <div class="cont_op">
        <div class="con_selet">
          <!-- 
      <div id="app">
        <div v-on:click="dodo">
            <button v-on:click.stop="doThis">阻止单击事件继续传播</button>
        </div>
      </div>
      <script>
        var app = new Vue({
            el: "#app",
            data: {
                name: "Vue.js"
            },
            methods: {
                doThis: function () {
                    alert("noclick");
                },
                dodo: function () {
                    alert("dodo");
                }
            }
        });//只输出"noclick"
      </script>
          -->
          <input
            type="checkbox"
            class="con_selectAll"
            :checked="isSelectedAll"
            @click.stop="selectedAll(isSelectedAll)"
          />
          <label for="con_selectAll">全选</label>
        </div>
        <div>商品信息</div>
        <div>单价</div>
        <div>数量</div>
        <div>金额</div>
        <div>操作</div>
      </div>
      <div class="pro_box">
        <div class="goods">
          <ul class="item_content">
            <li class="td td-chk">
              <div>
                <input type="checkbox" class="checkbox" />
              </div>
            </li>
            <li class="td td-item">
              <div class="td-item-pic">
                <img src="./../../common/img/logo-round.png" alt="" />
              </div>
              <div class="td-item-info">衣服</div>
            </li>
            <li class="td td-price"><strong>100元</strong></li>
            <li class="td td-amount">
              <div class="item-amount">
                <!-- 
                  el-input-number 计数器
                  1.指定最大、最小值
                  <el-input-number v-model="num2" 
                  :min="1" :max="10" label="请输入数量">
                  </el-input-number>
                  2.可以通过@change方法监控计数器的数值变化事件
                  <el-input-number v-model="num3" @change="numChange">
                  </el-input-number>
                  methods: {
                    numChange(value) {
                    console.log(value);
                    }
                  }
                 -->
                 <!-- <el-input-number
                  v-model="goods.buy_count"
                  :min="1"
                  :max="goods.counts"
                  @change="updateGoodsCount(goods, goods.buy_count)"
                ></el-input-number> -->
              </div>
            </li>
            <li class="td td-sum"><strong>120</strong></li>
            <li class="td td-op">
              <a href="" @click="clickTrash(goods)">删除</a>
            </li>
          </ul>
        </div>
      </div>
      <div class="pro_box">购物车里空空如也</div>
    </div>
    <div id="footer">
      <div class="foot_select">
        <input
          type="checkbox"
          name="foot_selectAll"
          class="foot_selectAll"
          :checked="isSelectedAll"
          @click.stop="selectedAll(isSelectedAll)"
        />
        <label for="foot_selectAll">全选</label>
      </div>
      <div class="foot_op">
        <!-- @click:绑定监听 ;@click.stop 阻止事件冒泡 ;@click.prevent 阻止事件的默认行为 -->
        <!-- 
          <a href="http://www.baidu.com" @click.prevent="test4">百度一下</a>   //阻止a标签跳转，仅执行函数test4
          <form  action="/xxx"   @submit.prevent="test5">
          //阻止表单提交，仅执行函数test5
          <input type="submit" value="注册">
          </form>
        -->
        <a href="" class="foot_remove" @click.prevent="emptyCart">清空购物车</a>
      </div>
      <div class="foot_total">
        <div class="amount-sum">
          <span class="txt">已选商品</span>
          <strong id="slected_amount">2</strong>
          <span class="txt">件</span>
        </div>
        <div class="price-sum">
          <span class="txt">合计（不含运费）：</span>
          <strong class="slected_price">200</strong>
        </div>
        <div class="btn-area">
          <a href="" class="btn-submit" :class="{ 'btn-allow': totalAmount }"
            >结算</a
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { mapActions } from 'vuex';
import { MessageBox } from 'element-ui';

export default {
  data() {
    return {
      shopsNum: [],
      isSelectedAll: false,
      totalPrice: 0, // 商品总价
      currentDelGoods: {}, // 当前删除的商品
      totalAmount: 0,
    };
  },
  computed: {
    ...mapState(["userInfo", "cartgoods"]),
  },
  mounted() {
    let user_id=this.userInfo.id;
    //请求商品数据
    //this.$store.dispatch() ：含有异步操作，例如向后台提交数据，写法：this.$store.dispatch(‘action方法名’,值)
    //this.$store.commit()：同步操作，可用于读取用户信息写到缓存里，写法：this.$store.commit(‘mutations方法名’,值)
    this.$store.dispatch("reqCartsGoods",{user_id});
  },
  methods:{
    //1.更新单个商品数量
    updateGoodsCount(goods,count){
      let user_id=this.userInfo.id;
      this.$store.dispatch("updateGoodsCount",{goods,count,user_id});
      this.getAllGoodsPrice();
    },
    //2.是否选中所有商品
    selectedAll(isSelectedAll){
      this.isSelectedAll=!isSelectedAll;
      this.$store.dispatch('selectedAll',{isSelectedAll});
      this.getAllGoodsPrice();
    },
    //3.计算商品总价
    getAllGoodsPrice(){
      let totalPrice=0;
      this.cartgoods.forEach((goods,index) => {
        if(goods.checked){
          totalPrice += goods.price / 100 * goods.buy_count;
        }
      });
      this.totalPrice=totalPrice;
    },
    //4.单个商品是否选中
    singleSelected(goods){
      this.$store.dispatch('singleSelected',{goods});
      this.hasSelectedAll();
      this.getAllGoodsPrice();
    },
    //5.判断是否全选
    hasSelectedAll(){
      let flag=true;
      let totalNum=0;
      this.cartgoods.forEach((goods,index)=>{
        if(!goods.checked){
          flag=false;
        }else{
          totalNum += 1;
        }
      });
      this.totalAmount=totalNum;
      this.isSelectedAll=flag;
    },
    //6.删除单个商品
    clickTrash(goods){
      this.$confirm('您确定删除该商品吗？','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).then(()=>{
        let user_id=this.userInfo.id;
        this.currentDelGoods=goods;
        this.$store.dispatch('delSingleGoods',{goods,user_id});
        this.getAllGoodsPrice();
      }).catch(()=>{
        this.$message({
          type:'info',
          message:'已取消删除'
        });
      });
    },
    //7.删除所有商品
    emptyCart(){
      this.$confirm('您确定删除所有商品吗？','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).then(()=>{
        let user_id=this.userInfo.id;
        this.$store.dispatch('delAllGoods',{user_id});
        this.getAllGoodsPrice();
      }).catch(()=>{
        this.message({
          type:'info',
          message:'已取消删除'
        });
      });
    },
  },
}
</script>


<style scoped>
#content > .header {
  width: 100%;
  height: 90px;
  margin-top: 10px;
  position: relative;
}
.header > img {
  width: 80px;
  height: 80px;
  border: 1px solid gray;
  border-radius: 50%;
  cursor: pointer;
}
.header > span {
  display: inline-block;
  font-size: 25px;
  height: 35px;
  font-weight: 700;
  position: absolute;
  top: 30%;
  margin-left: 20px;
  cursor: pointer;
}
#content > .cont_title {
  margin: 10px auto;
  width: 80%;
  height: 35px;
  border-bottom: 2px solid #e6e6e6;
}
.cont_title > span {
  display: inline-block;
  font-weight: 700;
  font-size: 17px;
  height: 100%;
  width: 100px;
  text-align: center;
  line-height: 25px;
  cursor: pointer;
  color: #f40;
  border-bottom: 2px solid #f40;
}
#content > .cont_op {
  width: 80%;
  margin: 0 auto;
  height: 40px;
  display: flex;
  justify-content: space-around;
}
.cont_op > div {
  line-height: 40px;
}
.cont_op > div:nth-child(2) {
  width: 120px;
}
#content > .pro_box {
  margin: 0 auto;
  width: 80%;
}
.pro_box > .goods {
  background: #fcfcfc;
  width: 100%;
  height: 130px;
  border: 1px solid #cccccc;
}
.goods > .item_content {
  padding: 15px;
  list-style: none;

  display: flex;
  justify-content: space-around;
}
.item_content > .td-item {
  width: 180px;
  position: relative;
}
.td-item > div {
  display: inline-block;
}
.td-item > .td-item-pic > img {
  width: 80px;
  height: 80px;
}
.td-item > .td-item-info {
  position: absolute;
  width: 80px;
  line-height: 15px;
  font-size: 12px;
  top: 0;
  margin-left: 20px;
}
.item_content > .td-sum {
  color: #f40;
}
.td-amount > .item-amout {
  height: 25px;
}
.item-amout a {
  display: inline-block;
  height: 23px;
  width: 17px;
  border: 1px solid #e5e5e5;
  background: #f0f0f0;
  text-align: center;
  line-height: 23px;
  color: #444;
  cursor: pointer;
}
.item-amout a:hover {
  color: #f50;
  border-color: #f60;
}
.item-amout > .text_amount {
  width: 40px;
  height: 20px;
  text-align: center;
  display: inline-block;
}
.td-op a {
  color: black;
}
.td-op a:hover {
  cursor: pointer;
  color: #f40;
}
#footer {
  width: 80%;
  height: 50px;
  position: fixed;
  bottom: 0;
  left: 10%;
  background: #e5e5e5;
  z-index: 9999;
}
#footer div {
  display: inline-block;
}
#footer > .foot_select {
  width: 50px;
  height: 50px;
  line-height: 50px;
  padding-left: 5px;
  cursor: pointer;
  font-size: 14px;
}
.foot_select input,
.foot_select label {
  cursor: pointer;
}
.foot_op {
  line-height: 50px;
  height: 50px;
}
.foot_op > .foot_remove {
  margin-left: 25px;
  font-size: 14px;
  cursor: pointer;
}
.foot_remove:hover {
  color: #f40;
}
.foot_total {
  float: right;
}
.foot_total > .amout-sum {
  cursor: pointer;
  height: 50px;
  color: #3c3c3c;
}
.foot_total .txt {
  line-height: 50px;
  font-size: 14px;
}
.amout-sum > #selected_amout {
  padding: 0 5px;
  color: #f40;
  font-weight: 700;
  font-size: 18px;
}
.price-sum {
  margin-left: 50px;
}
.price-sum > .selected_price {
  color: #f40;
  font-weight: 700;
  font-size: 20px;
  line-height: 50px;
}
.btn-area > .btn-sumbit {
  display: inline-block;
  background: #b0b0b0;
  color: #fff;
  border-left: 1px solid #e7e7e7;
  width: 119px;
  height: 50px;
  cursor: not-allowed;
  text-align: center;
  line-height: 50px;
  font-size: 20px;
}
.btn-area > .btn-sumbit.btn-allow {
  background: #f22d00;
  cursor: pointer;
}
</style>